package api

import (
	__ "api-gateway/basic/activity_proto"
	"api-gateway/basic/inits"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"fmt"
	"github.com/gin-gonic/gin"
)

// AddDistributionData
// @Summary      二级分销设置表添加
// @Description  二级分销设置表添加
// @Tags         二级分销模块
// @Accept      json
// @Produce     json
// @Param        address  body   request.DistributionData  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Router       /distribution/addData [post]
func AddDistributionData(c *gin.Context) {
	var req request.DistributionData
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	add, err := inits.ActivityClient.Distribution(c, &__.DistributionReq{
		DistributionType: req.DistributionType,
		Status:           req.Status,
	})

	if err != nil {
		response.Error(c, 500, "添加设置失败", err.Error())
		return
	}
	response.Success(c, "添加设置成功", add.Id)
}

// TwoDetailDistribution
// @Summary      二级分销下下级查询
// @Description  二级分销下下级查询
// @Tags         二级分销模块
// @Accept      json
// @Produce     json
// @Param        address  query   request.DistributionSearchList  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Router       /distribution/two/detail [get]
func TwoDetailDistribution(c *gin.Context) {
	var req request.DistributionSearchList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	list, err := inits.ActivityClient.DistributionSearchList(c, &__.DistributionSearchListReq{
		UserId: req.UserId,
	})

	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", list)
}

// TwoProcessDetailDistribution
// @Summary      二级分销查询下下级过程查询
// @Description  二级分销查询下下级过程查询
// @Tags         二级分销模块
// @Accept      json
// @Produce     json
// @Param        address  query   request.DistributionSearchList  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Router       /distribution/two/process [get]
func TwoProcessDetailDistribution(c *gin.Context) {
	var req request.DistributionSearchList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	list, err := inits.ActivityClient.DistributionSearchListProcess(c, &__.DistributionSearchListProcessReq{
		UserId: req.UserId,
	})

	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", list)
}

// AddPosterDistribution
// @Summary      二级分析海报模板添加
// @Description  二级分析海报模板添加
// @Tags         二级分销模块
// @Accept      json
// @Produce     json
// @Param        address  body   request.AddDistributionPosterTemplate  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Router       /distribution/add/poster [post]
func AddPosterDistribution(c *gin.Context) {
	var req request.AddDistributionPosterTemplate
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	poster, err := inits.ActivityClient.DistributionPoster(c, &__.DistributionPosterReq{
		Id: req.Id,
	})
	if err != nil {
		response.Error(c, 500, "添加失败", err.Error())
		return
	}
	response.Success(c, "添加成功", poster.Id)
}

// PosterShowDistribution
// @Summary      二级分销海报展示
// @Description  二级分销海报展示
// @Tags         二级分销模块
// @Accept      json
// @Produce     json
// @Param        address  query   request.AddDistributionPosterTemplate  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Router       /distribution/poster/show [get]
func PosterShowDistribution(c *gin.Context) {
	var req request.AddDistributionPosterTemplate
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	poster, err := inits.ActivityClient.DistributionPosterShow(c, &__.DistributionPosterShowReq{
		Id: req.Id,
	})
	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", poster)
}

// AddUserQuCodePosterDistribution
// @Summary      二级分销用户海报生成
// @Description  二级分销用户海报生成
// @Tags         二级分销模块
// @Accept      json
// @Produce     json
// @Param        address  body   request.DistributionSearchList  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Router       /distribution//user/qu/code [post]
func AddUserQuCodePosterDistribution(c *gin.Context) {
	var req request.DistributionSearchList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	poster, err := inits.ActivityClient.AddUserDistributionPosterQuCode(c, &__.AddUserDistributionPosterQuCodeReq{
		UserId: req.UserId,
	})
	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	file, err := c.FormFile("file")
	if err != nil {
		response.Error(c, 400, "上传文件失败", err.Error())
		return
	}
	dst := "../../upload/img/" + file.Filename
	// 上传文件至指定的完整文件路径
	err = c.SaveUploadedFile(file, dst)
	if err != nil {
		response.Error(c, 500, "上传文件失败", err.Error())
		return
	}
	response.Success(c, "查询成功", fmt.Sprintf(poster.ImgUrl))
}

// 流水列表展示
func ListDistributionRunWater(c *gin.Context) {
	var req request.DistributionSearchList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	poster, err := inits.ActivityClient.DistributionRunWaterList(c, &__.DistributionRunWaterListReq{
		UserId: req.UserId,
	})
	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", poster)
}

// 排行榜
func DistributionSort(c *gin.Context) {
	var req request.SortDistribution
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	poster, err := inits.ActivityClient.DistributionSortList(c, &__.DistributionSortListReq{
		ListType: req.ListType,
		Limit:    req.Limit,
	})
	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", poster)
}
